var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var base64 = require('../../utils/Base64.js');
Page({
  data: {
    util: util,

    token: '',
    uid: '',
    userinfo: '',

    guiListData: '',
    colorSet: '',
  },

  isDebug: false,//这个是调试机柜
  demo: false,//这个是无二维码的情况下进入购买演示

  mendianId:'',//门店id
  timer: undefined,

  onLoad: function(options) {

    var that = this;
    that.isDebug = options.debug == 'true' ? true : false; 
    that.demo = options.demo == 'true' ? true : false; 
    that.mendianId = options.mendianid ? options.mendianid : '';

    wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
        });
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this;
    //获取颜色配置
    app.getColorSetData(function(color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });
  },

  onShow:function(){
    this.getGuiListData();
    if(this.timer == undefined){
      this.regetGuiListData();
    }
  },

  onUnload:function(){
    clearInterval(this.timer);
    this.timer = undefined;
  },

  regetGuiListData:function(){
    var that = this;
    this.timer = setTimeout(function () {
      console.log("----Countdown----");
      that.getGuiListData();
      that.regetGuiListData();
    }, 60000);
  },

  //设置titlebar 样式
  resetNavColor: function() {
    var bg_color = '#f0f0f0';
    var fg_color = '#000000';
    if (this.data.colorSet) {
      bg_color = this.data.colorSet.titlebg;
      fg_color = this.data.colorSet.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  //获取所有售货机sim信息
  getGuiSimData: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "allguinum"
      },
      success: function (responseData) {
        console.log("所有售货机SIM卡信息：", responseData);
        if (responseData.data.msg) {

        } else {
          let simCardInfo = responseData.data;
          let guiData = that.data.guiListData;
          simCardInfo.forEach(function (m, i) {
            let n = 0;
            for (; n < guiData.length; n++) {
              if (guiData[n].guiid == m.guiid) {
                guiData[n].simnum = m.guinum;
                break;
              }
            }
            
          });
          that.setData({
            guiListData: guiData
          })
        }
      }
    });
  },

  //获取所有售货机信息
  getGuiListData: function() {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getguilist",
        "mendianid": this.mendianId ? this.mendianId:""
      },
      success: function(responseData) {
        console.log("所有售货机列表：", responseData);
        if (responseData.data.msg) {

        } else {
          if(that.data.guiListData && that.data.guiListData.length > 0){
            let guiData = that.data.guiListData;
            let listData = responseData.data;
            listData.forEach(function (m, i) {
              let n = 0;
              for (; n < guiData.length; n++) {
                if (guiData[n].guiid == m.guiid) {
                  break;
                }
              }
              if (m.isalive) {
                guiData[n].onofftext += '1';
                guiData[n].ontime += 1;
              } else {
                guiData[n].onofftext += '0';
                guiData[n].offtime += 1;
              }

            });
            that.setData({
              guiListData: guiData
            })
          } else {
            let listData = responseData.data;
            let onLineDev = [];
            let offLineDev = [];
            listData.forEach(function (m, i) {
              let start = m.guiid.length - 4;
              let showguiid = m.guiid.substr(start, 4);
              m.title = m.title + showguiid;
              if (m.isalive) {
                onLineDev.push(m);
                m.onofftext = '1';
                m.ontime = 1;
                m.offtime = 0;
              } else {
                offLineDev.push(m);
                m.onofftext = "0";
                m.ontime = 0;
                m.offtime = 1;
              }
              
            });
            listData = onLineDev.concat(offLineDev);
            that.setData({
              guiListData: listData
            })
            that.getGuiSimData();
          }
        }
      }
    });
  },

  itemTouchAction: function(e){
    var guiId = e.currentTarget.dataset.guiid;
    var path = "/pages/manage_gui/manage_gui?guiid=" + guiId;
    if (this.isDebug) {
      path = "/pages/gui_debug/gui_debug?guiid=" + guiId;
    } else if (this.demo) {
      path = "/pages/gui/gui?guiid=" + guiId;
    }
    wx.navigateTo({
      url: path,
    })
  },

  goAtherPage: function(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.link
    })
  },
  goAtherPageRedirect: function(e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({
      url: url
    })
  },

  //保存formId
  submitFormId: function(e) {
    app.saveFormId(e.detail.formId);
  },


})